package com.test.vcharts.task;

import com.test.vcharts.service.IStockService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 工作
 *
 * @author wen
 */
@Component
public class KLineTask {
    private static final Logger log = LoggerFactory.getLogger(KLineTask.class);
    private static ScheduledExecutorService fixedThreadPool = Executors.newScheduledThreadPool(3);
    @Autowired
    private IStockService stockService;
    @Autowired
    private SimpMessagingTemplate messagingTemplate;

    @Value("${stock.topic.list}")
    private String stockTopicList;


    public void run(int delay, String stockCode) {
        if (delay > 0) {
            fixedThreadPool.scheduleWithFixedDelay(new Runnable() {
                @Override
                public void run() {
                    try {

                        List<Object> result = (List<Object>) stockService.queryResult(stockCode, delay);
                       messagingTemplate.convertAndSend(stockTopicList, result);
                        Thread.sleep(5000L);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }, 0, delay, TimeUnit.MINUTES);
        }

    }

}
